<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {



public function index()
		{
			if($_POST){
				$tel=$_POST['mobile'];
				$User=D("Userinfo");
				$uarr=$User->where('tel='.$tel)->count();
				    if ($uarr['tel']>0) {
				          echo "<script>
				                  alert('该电话已注册，请更换后重试');
				                  history.go(-1);
				                </script>";
				                die;
				    }

				  // echo '<pre>';print_r($_POST);print_r($_SESSION);
				  if($_POST['mobile']!=$_SESSION['mobile'] or $_POST['mobile_code']!=$_SESSION['mobile_code'] or empty($_POST['mobile']) or empty($_POST['mobile_code'])){
				    exit('手机验证码输入错误。'); 
				  }else{
				    $_SESSION['mobile'] = '';
				    $_SESSION['mobile_code'] = '';  
				            $data['tel']=I('post.mobile');
				            $data['password']=md5(I("pwd"));
				            $data['yqm']=md5(I('post.yqm'));
				            $data['createtime']=time();
				            $data=array_filter($data);
				            if (!empty($data)) {
				              $u=$User->add($data);
				              if ($u) {
				              		$_SESSION['id']=$u;
				                    echo "<script>
				                            alert('注册成功');
				                            location.href='/Member/index';
				                          </script>";
				                	die;
				              }else{
				                $u->getError();
				              }
				            }
				  }
				}
			$_SESSION['send_code'] = random(6,1);
	         $this->display();
	      }

			//短信
			 public function sms()
			{
			  header("Content-type:text/html; charset=UTF-8");

			$target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

			$mobile = $_POST['mobile'];
			$send_code = $_POST['send_code'];

			$mobile_code = random(4,1);
			if(empty($mobile)){
			  exit('手机号码不能为空');
			}

			if(empty($_SESSION['send_code']) or $send_code!=$_SESSION['send_code']){
			  //防用户恶意请求
			  exit('请求超时，请刷新页面后重试');
			}

			$post_data = "account=cf_yingteng&password=1234567&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$mobile_code."。请不要把验证码泄露给其他人。");
			//密码可以使用明文密码或使用32位MD5加密
			$gets =  xml_to_array(Post($post_data, $target));
			if($gets['SubmitResult']['code']==2){
			  $_SESSION['mobile'] = $mobile;
			  $_SESSION['mobile_code'] = $mobile_code;
			}
			echo $gets['SubmitResult']['msg'];
			}

    public function Login(){
    	 if(isset($_SESSION['username'])){
            $this->redirect('/Member');
        }else{

      		$this->display();
 		 }
    }
        //验证登录
    public function yanzheng(){
    	$verify = new \Think\Verify();
            //判断用户名密码
            $user = new \Home\Model\UserinfoModel();
            $res = $user->checkName(I("username"),I("pwd"));
            if($res === false){ 
                    echo "<script>
                            alert('用户名或密码错误');
                            history.go(-1);
                          </script>";
            }else{
            	session("id",$res['uid']);
            	session("username",$res['tel']);
            	session("expire",7200);
            	$this->redirect('/Member');

            }

   
               
    }
}